// var _industry1Dict=['End user','Consultant','Third Organization','System Integrator'];
avalon.validators.othersIndustry = {
	message: 'The other industry is required and must be less than 50 characters long!',
	get: function(value, field, next) {
		var industry = $("#industry").val();
		if((0 == industry) && (industry != "")) {
			if(value == '') {
				next(false);
			} else {
				var flag = value.length >= 50;
				next(!flag);
			}
		} else {
			next(true);
		}
		return value
	}
}
avalon.validators.myIndustry1Required={
	get: function(value, field, next) {
		// if(_industry1Dict.indexOf(addPage.companyType)!=-1) {
		if(true) {
			if(value ==''){
				next(false);
			} else {
				next(true);
			}
		}else {
			next(true);
		}
		return value
	}
}
var addPage = avalon.define({
	$id: "addPage",
	aaa: '',
	projectName: "",
	endCustomer: "",
	region: "",
	country: "",
	state: "",
	city: "",
	industry: "",
	description: "",
	othersIndustry: "",
	estSales: 0,
	industry1:'',
	industry1List:[],
	cityRequired:true,
	chooseIndustry: function() {
		var industry = this.industry;
		if(0 == industry) {
			this.othersIndustry = "";
			$("#others_industry_el").removeClass("mui-hidden");
		} else {
			$("#others_industry_el").addClass("mui-hidden");
		}
	},
	validate: {
		onValidateAll: function(reasons) {
			if(reasons.length) {
				mui.toast(reasons[0].getMessage());
			} else {
				// var form=$("#addForm").serialize();
				var form=$.getFormData('addForm');
				if(form.startTime!='')
					form.starttime=new Date(form.startTime);
				if(form.endTime!='')
					form.endtime=new Date(form.endTime);
				if(form.region!='')
					form['region.name']=$('#region').find('option:selected').text();
				if(form.country!='')
					form['country.name']=$('#country').find('option:selected').text();
				if(form.state!='')
					form['state.name']=$('#state').find('option:selected').text();
				if(form.city!='')
					form['city.name']=$('#city').find('option:selected').text();
				console.log(form);
				mui.ajax('/project/add.do', {
					data: form,
					dataType: 'json',
					type:'post',
					success: function(data) {
						 mui.alert("",data.msg);
					}
				});

			}
		}

	},
	add: function(e) {
		e.preventDefault()
		this.validate.onManual();
	}
});

mui.ready(function() {

	$("#locations").cxSelect({
		selects: ["region", "country", "state", "city"],
		required:false,
		firstTitle:$.i18n.prop("Please_choose")
	});
	mui.ajax('/registry.do', {
		data: {
			'name':'CUSTOMER_CITY_REQUIRED'
		},
		dataType: 'json',
		type: 'post',
		success: function(data) {
			if(null!=data&&data.CUSTOMER_CITY_REQUIRED){
				if(data.CUSTOMER_CITY_REQUIRED=='1') {
					addPage.cityRequired=true;
				}else {
					addPage.cityRequired=false;
				}
			}
			initCityRequired();
		}
	});
	mui.ajax("/dict/get.json",{
		dataType: 'json',
		type: 'post',
		data: {
			parentId: '595'
		},
		success: function(data) {
			addPage.industry1List=data;
		}
	});
	function initCityRequired() {
		if(addPage.cityRequired) {
			$('#city').on('cxselect-init', function(e, r, d){
				if(r) {
					$('#city_sup').show();
					$(this).attr('ms-rules', '{required: true}');
					// addPage.cityRequired=true;
				}else {
					$('#city_sup').hide();
					$(this).attr('ms-rules', '{required: false}');
					// addPage.cityRequired=false;
				}
			});
		}else {
			$('#city_sup').hide();
		}
	}
});